import 'dart:async';

import 'package:flutter/material.dart';

class _CountBLoc {
  late int _count;
  get count => _count;

  late StreamController<int> _countController;

  _CountBLoc() {
    _count = 0;
//    _countController = StreamController<int>();
    _countController = StreamController<int>.broadcast();
  }

  Stream<int> get value => _countController.stream;

  add() {
    _countController.sink.add(++_count);
  }

  dispose() {
    _countController.close();
  }
}

// 全局单例
final _globBloc = _CountBLoc();

class BLoCDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _BLoCDemoState();
  }
}

class _BLoCDemoState extends State<BLoCDemo>
    with SingleTickerProviderStateMixin {
  var _tabctr;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _tabctr = TabController(length: 2, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('BLoCDemo'),
        bottom: TabBar(
          tabs: [
            Tab(
              text: 'item1',
            ),
            Tab(
              text: 'item2',
            ),
          ],
          controller: _tabctr,
        ),
      ),
      body: TabBarView(
        children: [
          _FirstPage(),
          _FirstPage(),
        ],
        controller: _tabctr,
      ),
    );
  }
}

class _FirstPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return StreamBuilder<int>(
      stream: _globBloc.value,
      initialData: _globBloc.count,
      builder: (context, snapshot) {

        var detail;

        switch (snapshot.connectionState) {
          case ConnectionState.none:
            detail = 'none(${snapshot.data}) 未设置可执行的future方法';
            break;
          case ConnectionState.waiting:
            detail ='waiting(${snapshot.data}) 正在执行';
            break;
          case ConnectionState.active:
            detail = 'active(${snapshot.data}) 有返回数据，但是并没有完成操作。比如Stream已传递数据，但是还在等待返回更多数据';
            break;
          case ConnectionState.done:
            detail = 'done(${snapshot.data}) 执行完成（应该是流关闭后）';
            break;
          default:
            detail = 'default ';
            break;
        }


        return Center(
          child: Column(
            children: <Widget>[
              Text(
                'count = ${snapshot.data}',
                style: Theme.of(context).textTheme.bodyLarge,
              ),
              Text(
                '',
              ),
              Padding(
                padding: EdgeInsets.all(30),
                child: FloatingActionButton(
                  onPressed: () => _globBloc.add(),
                  child: Icon(Icons.add),
                ),
              ),
              Text(
                '',
              ),
              Text(detail,),
            ],
          ),
        );
      },
    );
  }
}
